package com.tianqu.tms.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.dto.CheckInfoDto;

public class CheckInfoDaoImp extends TmsDao implements CheckInfoDao {

	protected CheckInfoDaoImp() throws TmsDaoException {
		super();
	}

	/* （非 Javadoc）
	 * @see com.tianqu.tms.dao.CheckInfoDao#saveCheckcarInfo(com.tianqu.tms.dto.CheckInfoDto)
	 */
	@Override
	public void saveCheckcarInfo(CheckInfoDto dto) throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer("");
		sql.append("	INSERT INTO	");
		sql.append(TmsTableNames.TMS_CHECK_CAR_INFO);
		sql.append("  ( ");
		sql.append("	SERIAL,	");
		sql.append("	VIN,	");
		sql.append("	LICENSEPLATE,	");
		sql.append("	CARDTYPE,	");
		sql.append("	OWNERTYPE,	");
		sql.append("	CAROWNER,	");
		sql.append("	OWNERADDRESS,	");
		sql.append("	OWNERORGNO,	");
		sql.append("	INSERTUSERID,	");
		sql.append("	UPDATEUSERID,	");
		sql.append("	INSERTTIME,	");
		sql.append("	UPDATETIME,");
		sql.append("	CAROWNERIDCARDNO	");
		sql.append(")VALUES(");
		sql.append("?,?,?,?,?,?,?,?,?,?,SYSDATE(),SYSDATE(),?");
		sql.append(")");
		try {
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, dto.getSerial());
			ps.setString(2, dto.getVin());
			ps.setString(3, dto.getLicensePlate());
			ps.setInt(4, dto.getCardType());
			ps.setInt(5, dto.getOwnerType());
			ps.setString(6, dto.getCarOwner());
			ps.setString(7, dto.getOwnerAddress());
			ps.setString(8, dto.getOwnerOrgNo());
			ps.setInt(9, dto.getInsertUserId());
			ps.setInt(10, dto.getUpdateUserId());
			ps.setString(11, dto.getCarOwnerIdCardNo());
			ps.executeUpdate();
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}
	}
	
	@Override
	public CheckInfoDto getCheckInfo(String serial)throws TmsDaoException{
		PreparedStatement ps = null;
		ResultSet rs = null;
		CheckInfoDto dto =null;
		StringBuffer sql = new StringBuffer("");
		sql.append("	SELECT	");
		sql.append("	*	");
		sql.append("	FROM	");
		sql.append(TmsTableNames.TMS_CHECK_CAR_INFO);
		sql.append("	WHERE	");
		sql.append("	SERIAL = '"+serial+"'	");
		try {
		ps = conn.prepareStatement(sql.toString());
		// 执行sql
		rs = ps.executeQuery();
        while (rs.next()) {
        	dto = new CheckInfoDto();
        	dto.setSerial(rs.getString("SERIAL"));
        	dto.setLicensePlate(rs.getString("LICENSEPLATE"));
        	dto.setVin(rs.getString("VIN"));
        	dto.setCardType(rs.getInt("CARDTYPE"));
			dto.setCarOwner(rs.getString("CAROWNER"));
			dto.setOwnerType(rs.getInt("OWNERTYPE"));
			dto.setCarOwnerIdCardNo(rs.getString("CAROWNERIDCARDNO"));
			dto.setOwnerAddress(rs.getString("OWNERADDRESS"));
			dto.setOwnerOrgNo(rs.getString("OWNERORGNO"));
		}
        return dto;
		}catch (SQLException e) {
			throw new TmsDaoException(e.toString());
		} finally {
			close(rs,ps);
		}
	}
	
	
	
	

	@Override
	public boolean checkSerialExist(String serial) throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		boolean bsflag = true;
		
		try {
			conn = super.getConnection();
			
			StringBuffer sql = new StringBuffer("");
			sql.append("	SELECT	");
			sql.append("	SERIAL	");
			sql.append("	FROM	");
			sql.append(TmsTableNames.TMS_CHECK_CAR_INFO);
			sql.append("	WHERE	");
			sql.append("	SERIAL = '"+serial+"'	");

			ps = conn.prepareStatement(sql.toString());
			// 执行sql
			rs = ps.executeQuery();

			while (rs.next()) { 
				if(rs.getString("SERIAL") !=null && !"".equals(rs.getString("SERIAL")))
				{
					bsflag =false;//不打打
				}
			}
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());
		} finally {
			close(rs,ps);
		}
		return bsflag;
	}

}
